//
//  TZNeteaseViewController.m
//  wangYiLottery
//
//  Created by mac on 16/3/2.
//  Copyright © 2016年 tz. All rights reserved.
//

#import "TZNeteaseViewController.h"
#import "TZNetModel.h"
#import "TZNetCell.h"
@interface TZNeteaseViewController ()
@property (nonatomic, strong) NSArray *netArray;
@end

@implementation TZNeteaseViewController

static NSString * const reuseIdentifier = @"netCell";

- (void)viewDidLoad {
    [super viewDidLoad];
    self.navigationItem.title = @"产品推荐";
    //加载xib
    UINib *nib = [UINib nibWithNibName:@"TZNetCell" bundle:nil];
    //注册单元格
    [self.collectionView registerNib:nib forCellWithReuseIdentifier:reuseIdentifier];
    //设置collection的背景颜色
    self.collectionView.backgroundColor = [UIColor colorWithRed:0.93 green:0.93 blue:0.91 alpha:1];
    
}

//cell有多少行
- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
    return self.netArray.count;
}
//collection长什么样
- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
    //去缓存池找
    TZNetCell *cell = [collectionView dequeueReusableCellWithReuseIdentifier:reuseIdentifier forIndexPath:indexPath];
    //把数据赋值给cell
    cell.model = self.netArray[indexPath.row];
    
    return cell;
}
//cell的点击事件
- (void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath {
    TZNetModel *p = self.netArray[indexPath.row];
//#warning mark - 跳转至相应的程序还是苹果商店
    //获取app对象
    UIApplication *app = [UIApplication sharedApplication];
    //应用程序的url
    NSURL *appUrl = [NSURL URLWithString:[NSString stringWithFormat:@"%@://%@",p.customUrl,p.ids]];
    
    //应用商店的url
    NSURL *storeUrl = [NSURL URLWithString:p.url];
    //---------iOS9----------------
        if (![app openURL:appUrl]) {
            //跳转到苹果商店
            [app openURL:storeUrl];
        }
//    //---------iOS8----------------
//    //判断是不是可以跳转到应用程序
//    if ([app canOpenURL:appUrl]) {
//        //可以打开应用程序
//        [app openURL:appUrl];
//    } else {
//        //不可以打开应用程序，则跳到苹果应用商店
//        [app openURL:storeUrl];
//    }
    
}

- (instancetype)init {
    //创建layout
    UICollectionViewFlowLayout *layout = [[UICollectionViewFlowLayout alloc] init];
    
    layout.itemSize = CGSizeMake(80, 80);//大小
    layout.minimumLineSpacing = 16;//行间距
    layout.minimumInteritemSpacing = 0;//左右最小间距
    layout.sectionInset = UIEdgeInsetsMake(16, 0, 0, 0);//组内边距
    
    return [super initWithCollectionViewLayout:layout];
    
}
//- (instancetype)initWithCollectionViewLayout:(UICollectionViewLayout *)layout {
//    return [self init];
//}

- (NSArray *)netArray {
    if (!_netArray) {
        //获取文件路径
        NSString *path = [[NSBundle mainBundle] pathForResource:@"more_project" ofType:@"json"];
        //把文件转化成data
        NSData *data = [NSData dataWithContentsOfFile:path];
        //通过data转成临时数组
        NSArray *tempArray = [NSJSONSerialization JSONObjectWithData:data options:NSJSONReadingMutableContainers error:nil];
        //初始化一个可变数组
        NSMutableArray *muArray = [NSMutableArray array];
        //遍历临时数组
        for (NSDictionary *dict in tempArray) {
            //字典转模型
            TZNetModel *model = [TZNetModel netModelWithDict:dict];
            [muArray addObject:model];
        }
        _netArray = muArray;
    }
    return _netArray;
}

@end
